/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.generalfile.logic;

/**
 *
 *
 * @author ShiYun,2014/11/05
 */
public class DataChange {

    public static String dec2Hex_2(int dec) {
        String s = Integer.toHexString(dec);
        char[] arr = s.toCharArray();
        if (arr.length == 1) {
            s = "0x0" + s;
        } else {
            s = "0x" + s;
        }
        return s;
    }

    public static String dec2Hex_4(long dec) {
        String s = Long.toHexString(dec);
        char[] arr = s.toCharArray();
        if (arr.length == 1) {
            s = "0x000" + s;
        } else if (arr.length == 2) {
            s = "0x00" + s;
        } else if (arr.length == 3) {
            s = "0x0" + s;
        } else {
            s = "0x" + s;
        }
        return s;
    }

    public static int hex2dec(String str_hex) {
        if (str_hex.startsWith("0x")) {
            String x = str_hex.substring(str_hex.indexOf("x") + 1);
            int a = Integer.parseInt(x, 16);
            return a;
        }
        return 0;
    }

    public static void main(String[] args) {
        int x = hex2dec("0xff");
        System.out.println("x=" + x);
    }
}
